package com.lc.recovery.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppFragment;
import com.lc.recovery.http.glide.GlideApp;
import com.lc.recovery.http.request.StorageMyApi;
import com.lc.recovery.http.response.StorageMyBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.ui.activity.HomeActivity;
import com.lc.recovery.ui.activity.LoginActivity;
import com.lc.recovery.ui.activity.VipCodeActivity;
import com.lc.recovery.ui.activity.recoverymine.RecoverySettingActivity;
import com.lc.recovery.ui.activity.warehousehome.WarehousingRecordNewActivity;
import com.lc.recovery.ui.activity.warehousemine.OutboundRecordActivity;
import com.lc.recovery.util.CourseUtils;
import com.lc.recovery.util.SPUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class MineWarehouseFragment extends AppFragment<HomeActivity> {
    @BindView(R.id.my_pic)
    ImageView myPic;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_leiji_money)
    TextView tvLeijiMoney;
    @BindView(R.id.tv_yue_money)
    TextView tvYueMoney;
    private String code_url;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_minewarehouse;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
    @Override
    public void onStart() {
        super.onStart();
        if ("".equals(SPUtil.get(getContext(), IntentKey.USERID, "")) || SPUtil.get(getContext(), IntentKey.USERID, "") == null || SPUtil.get(getContext(), IntentKey.USERID, "").isEmpty()) {
            tvLogin.setVisibility(View.VISIBLE);
            tvName.setVisibility(View.GONE);
            tvPhone.setVisibility(View.GONE);
            myPic.setImageResource(R.mipmap.mine_default_avatar);
        }else {
            memberInfo();


        }

    }
    private void memberInfo() {
        EasyHttp.post(this)
                .api(new StorageMyApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, ""))).request(new HttpCallback<StorageMyBean>(this) {

            @Override
            public void onSucceed(StorageMyBean data) {
                if (data.getCode() == 200) {
                    tvLogin.setVisibility(View.GONE);
                    tvName.setVisibility(View.VISIBLE);
                    tvPhone.setVisibility(View.VISIBLE);
                    tvName.setText(data.getData().getReal_name());
                    tvPhone.setText(data.getData().getMobile());
                    SPUtil.put(getContext(), IntentKey.PHONE, data.getData().getMobile());
                    SPUtil.put(getContext(), IntentKey.NAME, data.getData().getReal_name());
                    if (data.getData().getAvatar()!=null) {
                        SPUtil.put(getContext(), IntentKey.IMAGE, data.getData().getAvatar());
                    }
                    GlideApp.with(getActivity())
                            .load(data.getData().getAvatar())
                            .placeholder(R.mipmap.avatar_placeholder_ic)
                            .error(R.mipmap.avatar_placeholder_ic)
                            .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                            .into(myPic);
                    tvLeijiMoney.setText(data.getData().getStock_out()+"");
                    tvYueMoney.setText(data.getData().getStock_in()+"");
                    code_url = (data.getData().getEwm());
                    SPUtil.put(getContext(),IntentKey.URL,data.getData().getEwm());
                }

            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }
    @OnClick({R.id.my_pic, R.id.tv_login, R.id.iv_setting, R.id.iv_huiyuama, R.id.ll_chuku, R.id.ll_ruku})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_pic:
                break;
            case R.id.tv_login:
                startActivity(LoginActivity.class);
                break;
            case R.id.iv_setting:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginActivity.class);
                    return;
                }
                startActivity(RecoverySettingActivity.class);
                break;
            case R.id.iv_huiyuama:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginActivity.class);
                    return;
                }
                startActivity(new Intent(getContext(), VipCodeActivity.class).putExtra("code_url",code_url));
                break;
            case R.id.ll_chuku:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginActivity.class);
                    return;
                }
                startActivity(OutboundRecordActivity.class);

                break;
            case R.id.ll_ruku:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginActivity.class);
                    return;
                }
                startActivity(WarehousingRecordNewActivity.class);

                break;
        }
    }
}
